

package server;

import java.io.IOException;
import java.net.ServerSocket;
import javax.net.ServerSocketFactory;

/**
 *
 * @author Muhammad Asad Ullah
 */
public class connectionListener extends Thread{

    /**
     * Main Socket through which Server accepts connection.
     */
    private  ServerSocket listerSocket;
    /**
     *  reference To main Server Object.
     */
    private Server parentServerObject;
    /**
     *  Boolean check for listening
     */
    private Boolean keepRunning;
    
    /**
     *
     * @param parent
     * @throws IOException
     * Constructor initilized an instance of listener which accepts incomeing
     * connections to the server and pass resulting socket to Server for Handling.
     */
    public connectionListener(Server parent) throws IOException
    {
        this.keepRunning = true;
        this.parentServerObject = parent;
        ServerSocketFactory factory = ServerSocketFactory.getDefault();        
        this.listerSocket = factory.createServerSocket(4000);
        this.start();//Start listending for requests asynchronously.




    }

    @Override
    /**
     * Run method which asynchronously listens for incoming connection requests.
     */
    public void run()
    {
        System.out.println("Server running on port 4000");
        while(this.keepRunning)
        {
            try
            {
                //Pass the Socket resulting from client connection to Server to handle...
                this.parentServerObject.addNewClient(this.listerSocket.accept());
                
            }
            catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
        }

    }

   


}
